package com.wnx.job.modules.nms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnx.job.common.exception.Asserts;
import com.wnx.job.common.mapstruct.ChannelMapper;
import com.wnx.job.modules.nms.dto.ChannelParam;
import com.wnx.job.modules.nms.mapper.NmsChannelMapper;
import com.wnx.job.modules.nms.model.NmsChannel;
import com.wnx.job.modules.nms.service.NmsArticleService;
import com.wnx.job.modules.nms.service.NmsChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-16
 */
@Service
public class NmsChannelServiceImpl extends ServiceImpl<NmsChannelMapper, NmsChannel> implements NmsChannelService {
    @Resource
    private NmsChannelMapper channelMapper;
    @Resource
    private NmsArticleService articleService;


    /***
     * 删除咨询栏目
     * @param id
     */
    @Override
    public void deleteChannel(Long id) {
        //删除校验：咨询栏目之前要确保子栏目的咨询栏目全部删除了！
        long count = this.findCountByPid(id);
        if (count > 0){
            Asserts.fail("该栏目下存在有子栏目，请先删除子栏目");}

        //删除校验：咨询栏目要确保 这个咨询栏目下没有新闻咨询
        long count2 = articleService.findCountByChannelId(id);
        if (count2 > 0){Asserts.fail("该栏目下还存在有咨询信息，请先删除咨询信息");}

        //调用持久层，根据栏目ID删除栏目
        channelMapper.deleteById(id);


    }

    /**
     * 根据此栏ID作为父ID查询咨询栏目数量
     * @param id
     * @return
     */
    @Override
    public long findCountByPid(Long id) {
        return  channelMapper.selectCount(new QueryWrapper<NmsChannel>().eq("parent_id",id));
    }

    /**
     * 获取咨询树集合
     * @return
     */
    @Override
    public List<ChannelParam> getChannelTree() {
        //调用持久层，获取全部咨询栏目集合
        List<NmsChannel> tbChannels = channelMapper.selectList(null);
        //属性复制
        List<ChannelParam> channelParamList = ChannelMapper.INSTANCE.toDtoList(tbChannels);

        //转成树状结构
        List<ChannelParam> result = channelParamList.stream()
                .filter(permission -> permission.getParentId().equals(0L))
                .map(permission -> buildTreeChannel(permission, channelParamList))
                .collect(Collectors.toList());
        return  result;
    }

    /**
     * 根据栏目位置获取咨询栏目树
     * @param position
     * @return
     */
    @Override
    public List<ChannelParam> findChannelTreeByPosition(int position) {
        List<NmsChannel> tbChannels = channelMapper.selectList(new QueryWrapper<NmsChannel>().eq("position",position));
        //属性复制
        List<ChannelParam> channelParamList = ChannelMapper.INSTANCE.toDtoList(tbChannels);
        //转成树状结构
        List<ChannelParam> result = channelParamList.stream()
                .filter(permission -> permission.getParentId().equals(0L))
                .map(permission -> buildTreeChannel(permission, channelParamList))
                .collect(Collectors.toList());
        return  result;

    }

    private ChannelParam buildTreeChannel(ChannelParam permission, List<ChannelParam> channelParamList) {
        ChannelParam node = new ChannelParam();
        BeanUtils.copyProperties(permission, node);
        List<ChannelParam> children = channelParamList.stream()
                .filter(subPermission -> subPermission.getParentId().equals(permission.getId()))
                .map(subPermission -> buildTreeChannel(subPermission, channelParamList))
                .collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }


}
